<?php
require_once 'includes/image_resizer.php'; 

$smsg="";
if($_SESSION['user_id']=="") {
	echo "<script type='text/javascript'> window.location='index.php?signin'; </script>";
}
$userId = $_SESSION['user_id'];
$userType = $_SESSION['user_type'];

if(!empty($_REQUEST['keyword']))
{
	$curProId = $_REQUEST['keyword'];
	$today=date("Y-m-d G:i:s",time());
	$duration=$_REQUEST['pro_duration'];
	$pro_quantity=$_REQUEST['pro_quantity'];
	$price=$_REQUEST['price'];		
	$category="";
	
	if($_REQUEST['pro_sub_subcategory']!="") {
		$category=$_REQUEST['pro_sub_subcategory'];
	}
	else {
		$category=$_REQUEST['pro_subcategory'];
	}
	$pro_description=$_REQUEST['pro_description'];	
	$sql_addpro="UPDATE tbl_wholeseller_product set 
				category_id='$category',
				quantity='$pro_quantity',
				product_price='$price',
				product_description='$pro_description' 					
				where product_id='$curProId'";
	$rst_addpro=mysql_query($sql_addpro);
	//check product entry in tbl_product_images
	$sqlChk = "SELECT product_id FROM tbl_product_images WHERE product_Id='$curProId' AND  product_type='p'";
	$rstChk = mysql_query($sqlChk) or die(mysql_error());
	if(mysql_num_rows($rstChk)==0) {
		$sqlInsEntry = "INSERT INTO tbl_product_images SET product_id='$curProId', product_type='p'";
		$rstInsEntry = mysql_query($sqlInsEntry) or die(mysql_error()); 
	}
	
	if($rst_addpro) {
		$smsg="true";
		$pro_id=$curProId;
		
		$imgPaths = getImagePath($pro_id); 
		
		for($i=1; $i<=5;$i++) {
			$pro_image[$i]=pathinfo($_FILES["pro_image".$i]["name"]);	
					
			if(!empty($pro_image[$i]['extension'])) {
				if(strtolower($pro_image[$i]['extension'])=="jpeg" || strtolower($pro_image[$i]['extension'])=="jpg" || strtolower($pro_image[$i]['extension'])=="gif") {
					
					/* if($imgPaths!="") {															
							if($imgPaths['product_image_'.$i]!="") { unlink($imgPaths['product_image_'.$i]); }
							if($imgPaths['product_sample_'.$i]!="") { unlink($imgPaths['product_sample_'.$i]); }
							if($imgPaths['product_thumbnail_'.$i]!="") { unlink($imgPaths['product_thumbnail_'.$i]); }
						
					} */
						
					$orgname[$i]=mt_rand();				
					$type[$i]=$pro_image[$i]['extension'];				
					$path[$i]="products/original/".$orgname[$i].$pro_id.".".$type[$i];								
					
					$path_thumb[$i]="products/thumb/".$orgname[$i].$pro_id."_thumb.".$type[$i];						
					
					$path_medium[$i]="products/medium/".$orgname[$i].$pro_id."_medium.".$type[$i];				
					
					move_uploaded_file($_FILES["pro_image".$i]["tmp_name"],$path[$i]);				  
					productResize($path[$i], $path_thumb[$i], $path_medium[$i], $type[$i]);	
					$imagePath[] = 	$path[$i];	
					$mediumPath[] = $path_medium[$i];	
					$thumbPath[] = 	$path_thumb[$i];	
					$sql_updpro="UPDATE tbl_product_images set list_type='1',					
								 product_thumbnail_$i='".$path_thumb[$i]."',
								 product_sample_$i='".$path_medium[$i]."',
								 product_image_$i='".$path[$i]."'
								 where product_id=$pro_id AND product_type='p'";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()>0) {
						
					}
					else { $smsg="false"; }		
				}
			 }
		 } //end of for
	}
	else { $smsg="false"; }	
}


$proId = $_GET['editwholeitem'];
$owner = getProductOwner($proId);


$sqlSelPro = "SELECT * FROM tbl_wholeseller_product as tp left join tbl_product_images as tpi ON tp.product_id=tpi.product_id where tp.product_id='$proId'";
$rstSelPro = mysql_query($sqlSelPro);
$rowSelPro = mysql_fetch_array($rstSelPro);
$cId = getCategory($rowSelPro['category_id']);

//print_r($cId);
//exit;
?>
<style type="text/css">
	#sell-form { border:0px solid #CCC; padding:10px; float:left; vertical-align:top; width:93%; margin:2%; }

	#sell-form label{
	float: left;
	width: 130px;
	font-weight: bold;
	}
	
	#sell-form input, select, textarea{	
	width: 280px;
	margin-bottom: 5px;
	font-size:12px;	
	}
	
	#sell-form textarea{
	height: 250px;
	width: 450px;
	font-size:12px;
	}
	
	#sell-form a{
		float:left;
	}
	
	#sell-form #submitbutton{
	margin-left: 130px;
	margin-top: 5px;
	width: 90px;
	}
	
	#sell-form #sellby-div { float:left;  }
	
	#sell-form #sellby-div label { width: 70px; margin-bottom:2px; font-weight:normal; }
	#sell-form #sellby-div input { width: 1px; }
	
	#sell-form br{
	clear: left;
	}

	
</style>
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Edit Product</h2></td>
  </tr>
  <tr>
    <td>
	
	<table width="100%" align="center"  border="0" cellspacing="0">
      <tr>
	  	<td><?php if($smsg=="true") { echo "<div id='suc-msg'> Product updated successfully. </div>"; } ?></td>
	  </tr>
      <tr>
        <td height="500"><!-- CODEX technology start here -->
		<form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
           
			  <input type="hidden" name="keyword" id="keyword" value="yes">
				<div id="sell-form">
				 <label for="pro_name">Product Code</label>
					<b><?php echo $rowSelPro['product_code']; ?></b><br>
				 <label for="pro_name">Product Name</label>
					<b><?php echo $rowSelPro['product_name']; ?></b><br><br>
					  <label for='pro_subcategory'>Category</label>
					  
					  <?php							
							$qry="SELECT * FROM tbl_category where category_id='$cId[subcategoryId]'";
							$res=mysql_query($qry);
							$row = mysql_fetch_assoc($res);
							echo $row['category_name'];
							?><span id="loader-div1"></span><br>
					</span>
					<span id="sub-sub-category"> 
						<label for='pro_sub_subcategory'>Sub Category</label>
						<?php
							if($cId['subsubcategoryId']!="") {
							$qry="SELECT * FROM tbl_category where category_id='".$cId['subcategoryId']."'";
							$res=mysql_query($qry);
							$row = mysql_fetch_assoc($res);
							echo $row['category_name'];					
							} ?><br>
					</span>
					<label for="pro_description">Description</label>
					<textarea name="pro_description" id="pro_description"><?php echo $rowSelPro['product_description']; ?></textarea><br>					
					
				<label for="pro_image1">Product Price</label>
					<input type="text" name="price" id="price" value="<?php echo $rowSelPro['quantity']; ?>"><br>
					
					<label for="pro_image1">Product Image 1</label>
					<input type="file" name="pro_image1" id="pro_image1"><br>
					<label for="pro_image2">Product Image 2</label>
					<input type="file" name="pro_image2" id="pro_image2"><br>
					<label for="pro_image3">Product Image 3</label>
					<input type="file" name="pro_image3" id="pro_image3"><br>
					<label for="pro_image4">Product Image 4</label>
					<input type="file" name="pro_image4" id="pro_image4"><br>
					<label for="pro_image5">Product Image 5</label>
					<input type="file" name="pro_image5" id="pro_image5"><br>
					
					<label for="pro_quantity">Quantity</label>
					<input type="text" style="width:80px;" value="<?php echo $rowSelPro['quantity']; ?>" name="pro_quantity" id="pro_quantity"><br><input type="submit" id="submitbutton" name="submitbutton" value="Submit">
					<br />
				</div><br>
            
        </form></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>